import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.TimerTask;


public class ShootGame extends JPanel {


    public  static BufferedImage background = null;
    public  static BufferedImage hero0 = null;
    public  static BufferedImage hero1 = null;
    public  static BufferedImage bee0 = null;
    public  static BufferedImage airplane0 = null;
    public  static BufferedImage bigplane0 = null;
    public  static BufferedImage bullet0 = null;

    public static final int width = 400;
    public static final int height = 600;


    static{
        try{
            background = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/background.png"));
            hero0 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/hero0.png"));
            hero1 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/hero1.png"));
            bee0 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/bee.png"));
            airplane0 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/airplane.png"));
            bigplane0 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/bigplane.png"));
            bullet0 = ImageIO.read
                    (ShootGame.class.getResourceAsStream("pic/bullet.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    Hero hero = new Hero();
//    Bee bee = new Bee();
//    BigPlane bigPlane = new BigPlane();
//    AirPlane airPlane = new AirPlane();
//    Bullet bullet = new Bullet();
    ArrayList<Bullet> bullets = new ArrayList();
    ArrayList<FlyingObject> flyingObjects = new ArrayList();




    //增加成员方法、开始方法、执行定时且重复的内容
    private java.util.Timer timer = new java.util.Timer();
    public void action(){
        //参数一:TimerTask定时器的任务，要重复执行的代码
//        参数二：long 表示定时器开始的时间（ms)
//        参数三：long 时间间隔，隔多久时间再执行一次
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //所有定时且重复的内容在RUN（)中执行
//                生成新的飞行物
//                飞行物的移动

                hero.move();
//                airPlane.move();
//                bee.move();

//                生成很多飞行物
                createFlyingObject();

                //很多飞行物的移动
                flyingObjectStep();

                repaint();//刷新世界
            }
        },1000,5);
    }


    private void flyingObjectStep(){
        for(int i = 0;i < flyingObjects.size(); i++ ){
            FlyingObject fly = flyingObjects.get(i);
            fly.move();
        }
    }

    private int flyingIndex = 0;
    private void createFlyingObject() {
        flyingIndex++;
        if (flyingIndex % 20 == 0) {
            int ran = (int) (Math.random() * 20);
            FlyingObject fly;
            if (ran == 0 | ran == 1 | ran == 2) {
                fly = new Bee();//多态
            } else if (ran == 3 | ran == 4 | ran == 5 | ran == 6) {
                fly = new BigPlane();
            } else {
                fly = new AirPlane();
            }
            flyingObjects.add(fly);

        }
    }





    public void paint(Graphics g){
        super.paint(g);
        g.drawImage(hero.image,hero.x,hero.y,this);
//        g.drawImage(bee.image,bee.x,bee.y,this);
//        g.drawImage(bigPlane.image,bigPlane.x,bigPlane.y,this);
//        g.drawImage(bullet.image,bullet.x,bullet.y,this);
        paintFlyingObject(g);
    }

    private void paintFlyingObject(Graphics g) {
        for(int i = 0; i < flyingObjects.size(); i++){
            FlyingObject fly = flyingObjects.get(i);
            g.drawImage(fly.image,fly.x,fly.y,this);
        }
    }


    public static void main(String[] args) {
        //创建窗口对象
        JFrame win = new JFrame();
        //设置窗口标题
        win.setTitle("飞机大战");
        //设置窗口大小
        win.setSize(400,650);
        //居中显示
        win.setLocationRelativeTo(null);
        //设置窗口没有外边框
        //win.setUndecorated(true);
        //显示窗口
        win.setVisible(true);
        //设置窗口默认关闭选项
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //将画板添加到窗口上
        ShootGame panel = new ShootGame();
        win.add(panel);

        panel.action();

    }
}
